package com.yan.zrefreshview.slice;

import com.yan.zrefreshview.ResourceTable;
import com.yan.zrefreshview.ZRefreshView;
import com.yan.zrefreshview.bean.SampleItem;
import com.yan.zrefreshview.provider.SampleItemProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ListContainer;
import ohos.app.dispatcher.TaskDispatcher;

import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    private ZRefreshView refreshView;
    private ListContainer listContainer;
    private TaskDispatcher uiTaskDispatcher;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        uiTaskDispatcher = getUITaskDispatcher();
        refreshView = (ZRefreshView) findComponentById(ResourceTable.Id_zrefresh_view);
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
        refreshView.setOnRefreshListener(new ZRefreshView.RefreshListener() {
            @Override
            public void onPullRefreshing() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        uiTaskDispatcher.asyncDispatch(new Runnable() {
                            @Override
                            public void run() {
                                initListContainer();
                                refreshView.setShowDuration(1200);
                                refreshView.setHeaderTx("本次更新" + sampleItemProvider.getCount() + "条数据");
                                refreshView.finishRefreshing();
                            }
                        });
                    }
                }).start();
            }


        });

        refreshView.setLoadMoreListener(() -> {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    uiTaskDispatcher.asyncDispatch(new Runnable() {
                        @Override
                        public void run() {
                            int count = sampleItemProvider.getCount();
                            for (int i = count; i < count + 10; i++) {
                                list.add(new SampleItem("Item" + i));
                            }
                            sampleItemProvider.notifyDataChanged();
                            refreshView.finishLoadMore();
                        }
                    });
                }
            }).start();
        });
        initListContainer();
    }

    List<SampleItem> list;
    SampleItemProvider sampleItemProvider;

    private void initListContainer() {
        list = getData();
        sampleItemProvider = new SampleItemProvider(list, this);
        listContainer.setItemProvider(sampleItemProvider);
    }

    private ArrayList<SampleItem> getData() {
        ArrayList<SampleItem> list = new ArrayList<>();
        for (int i = 0; i <= 10; i++) {
            list.add(new SampleItem("Item" + i));
        }
        return list;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
